package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.hosp.mapper.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.utils.HttpRequestHelper;
import com.atguigu.yygh.model.hosp.BookingRule;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private DictFeignClient dictFeignClient;

    @Autowired
    private HospitalRepository hospitalRepository;


    @Override
    public void save(Map<String, Object> parameterMap) {
        //将map类型转换一下，方便后面的操作
        //将map集合转换成对象
        String json = JSONObject.toJSONString(parameterMap);
        Hospital hospital = JSONObject.parseObject(json, Hospital.class);

        //首先使用hoscode查询该医院是否已经存在，如果存在则使用修改，如果不存在使用添加
        Hospital hospital1 = hospitalRepository.findByHoscode(hospital.getHoscode());

        if(hospital1 != null){
            hospital.setId(hospital1.getId());

            hospital.setCreateTime(hospital1.getCreateTime());

            hospital.setUpdateTime(new Date());
            hospital.setStatus(1);
            hospitalRepository.save(hospital);

        }else{
            hospital.setCreateTime(new Date());

            hospital.setUpdateTime(new Date());
            hospital.setStatus(1);
            hospitalRepository.save(hospital);
        }

    }

    @Override
    public Hospital show(String hoscode) {
        return hospitalRepository.findByHoscode(hoscode);
    }

    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //创建排序
        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");
        //创建分页
        Pageable pageable = PageRequest.of(page-1,limit,sort);

        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写

        Hospital hospital = new Hospital();
        if(!StringUtils.isEmpty(hospitalQueryVo)){
            BeanUtils.copyProperties(hospitalQueryVo,hospital);
        }
        Example<Hospital> example = Example.of(hospital,matcher);

        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        pages.getContent().stream().forEach(item ->{
            this.getName(item);
        });

        return pages;
    }

    private Hospital getName(Hospital item) {

        String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(),item.getHostype());
        String provinceString = dictFeignClient.getName(item.getProvinceCode());
        String cityString = dictFeignClient.getName(item.getCityCode());
        String districtString = dictFeignClient.getName(item.getDistrictCode());

        item.getParam().put("hostypeString", hostypeString);
        item.getParam().put("fullAddress", provinceString + cityString + districtString + item.getAddress());

        return item;

    }

    @Override
    public void updateStatus(String id, Integer status) {
        //根据id查询
        Hospital hospital = hospitalRepository.findById(id).get();

        hospital.setStatus(status);

        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String, Object> showaa(String id) {
        Map<String,Object> map = new HashMap<>();

        Hospital hospital = hospitalRepository.findById(id).get();

        BookingRule bookingRule = hospital.getBookingRule();

        map.put("bookingRule",bookingRule);
        map.put("hospital",hospital);

        return map;
    }

    @Override
    public List<Hospital> findAllHosp(HospitalQueryVo hospitalQueryVo) {

        //创建条件
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);

        Example<Hospital> example = Example.of(hospital);
        List<Hospital> list = hospitalRepository.findAll(example);

        list.stream().forEach(item->{
            this.getName(item);
        });


        return list;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {

        List<Hospital> list = hospitalRepository.findHospByHosnameLike(hosname);

        return list;
    }

    @Override
    public Map<String, Object> item(String hoscode) {

        Map<String, Object> map = new HashMap<>();

        Hospital byHoscode = hospitalRepository.findByHoscode(hoscode);

        map.put("hospital",byHoscode);
        BookingRule bookingRule = byHoscode.getBookingRule();

        map.put("bookingRule",bookingRule);

        return map;
    }

    //根据医院编号查询医院名称
    @Override
    public String getHospName(String hoscode) {
        Hospital byHoscode = hospitalRepository.findByHoscode(hoscode);
        return byHoscode.getHosname();
    }
}
